import time

from .config_data.case_data import *
import pytest
from tool_class.page_handle import LabelManagePageHandle


@pytest.fixture(scope='module')
def tag_application_module(session_page):
    handle = LabelManagePageHandle(session_page)
    #进入标签库页面
    handle.goto_local(url_path="/ins/#/tagManagement/application")
    yield session_page, handle

@pytest.fixture(scope='function')
def tag_application_function(tag_application_module):
    session_page,handle = tag_application_module
    session_page.mouse.move(0, 0)
    session_page.mouse.click(x=0, y=0)
    yield session_page, handle
    session_page.mouse.move(0, 0)
    session_page.mouse.click(x=0, y=0)

@pytest.mark.parametrize("unit",[tag_application_query_01,tag_application_query_02,tag_application_query_03,
                                tag_application_query_04,tag_application_query_05,tag_application_query_06,])
@pytest.mark.run(order=700)
def test_tag_application_query_01(unit,tag_application_module):
    """ 查询标签应用列表 """
    session_page,handle = tag_application_module
    result = handle.query_handle(unit)
    keys = list(result.keys())
    first_key = keys[0]
    last_key = keys[-1]
    no_data = session_page.locator('.arco-empty-description')
    if no_data.is_visible():
        assert no_data.inner_text() == result[last_key] \
               and not session_page.get_by_role("alert").is_visible()
    elif "所属分类" in result:  # 查询结果中包含"所属分类"字段需要对数据进行分割在断言
        data_list = [itme.split('#') for itme in result['所属分类列表']]
        assert all(result['所属分类'] == itme for itme in data_list) \
               and not session_page.get_by_role("alert").is_visible()
    else:
        assert all(result[first_key] in itme for itme in result[last_key]) \
               and not session_page.get_by_role("alert").is_visible()
    handle.element_click("application-10007")  # 重置查询

@pytest.mark.run(order=701)
def test_tag_application_query_02(tag_application_module):
    """ 组合查询标签应用列表 """
    session_page,handle = tag_application_module
    result = handle.query_handle(tag_application_query_07)
    no_data = session_page.locator('.arco-empty-description')
    if no_data.is_visible():
        assert  no_data.inner_text() == result['标签名称列表'] \
                and not session_page.get_by_role("alert").is_visible()
    else:
        assert all(result['标签名称'] in itme for itme in result['标签名称列表'] ) \
               and all(result['启用状态'] in itme for itme in result['启用状态列表']) \
                and all(result['标签类型'] in itme for itme in result['标签类型列表'])\
               and all(result['能源类型'] in itme for itme in result['能源类型列表']) \
                and not session_page.get_by_role("alert").is_visible()
    handle.element_click("application-10007")  # 重置查询

@pytest.mark.parametrize("unit",[tag_application_query_08,tag_application_query_09,tag_application_query_10])
@pytest.mark.run(order=702)
def test_tag_application_query_03(tag_application_module,unit):
    """ 标签应用选择标签类型时级联所属分类 """
    session_page,handle = tag_application_module
    result = handle.query_handle(unit)
    tag_type_1 = session_page.get_by_role("menuitem", name='智能化体验')
    tag_type_2 = session_page.get_by_role("menuitem", name='电气')
    if "pull_checked.标签类型" not in unit: # 未选择标签类型时，所属分类不展示可选项
        assert not tag_type_1.is_visible() and not tag_type_2.is_visible()
    elif result['标签类型'] == '质量标签': # 选择标签类型时，所属分类展示可选项
        assert tag_type_2.is_visible() and not tag_type_1.is_visible()
    else:
        assert tag_type_1.is_visible() and not tag_type_2.is_visible()
    handle.element_click("application-10007")  # 重置查询

@pytest.mark.parametrize("unit",[tag_application_add_01,tag_application_add_02,tag_application_add_03,
                                 tag_application_add_04,tag_application_add_05,tag_application_add_06])
@pytest.mark.run(order=703)
def test_tag_application_add_01(tag_application_function,unit):
    """ 必填项验证 """
    session_page, handle = tag_application_function
    handle.element_click("application-20002")  # 点击新增标签
    result = handle.add_handle(unit)
    expect_value = next(iter(result))
    handle.button_click(text='确定')  # 点击确定按钮
    assert session_page.get_by_role("alert").inner_text() == expect_value

@pytest.mark.parametrize("unit",[tag_application_add_07,tag_application_add_08,
                                 tag_application_add_09,tag_application_add_10,tag_application_add_11])
@pytest.mark.run(order=704)
def test_tag_application_add_02(tag_application_function,unit):
    """ 验证字段长度 """
    session_page, handle = tag_application_function
    handle.element_click("application-20002")
    result = handle.add_handle(unit)
    keys = list(result.keys())
    first_key = keys[-1] # 获取最后一个字段名称
    expected_length = first_key.split('_')[-1]
    assert int(expected_length) == len(result[first_key])

@pytest.mark.parametrize("unit",["application-form-10002-0","application-form-10002-1"])
@pytest.mark.run(order=705)
def test_tag_application_add_03(tag_application_function,unit):
    """ 编辑页标签类型时级联所属分类 """
    session_page, handle = tag_application_function
    handle.element_click("application-20002")  # 点击新增
    result = handle.pull_checked("application-form-10002", unit)  # 选择标签类型
    tag_type_1 = session_page.get_by_role("menuitem", name='智能化体验')
    tag_type_2 = session_page.get_by_role("menuitem", name='电气')
    handle.element_click("application-form-10005")  # 所属分类
    if  result ==  "业务标签" :  # 未选择标签类型时，所属分类不展示可选项
        assert tag_type_1.is_visible() and not tag_type_2.is_visible()
    elif result == '质量标签':  # 选择标签类型时，所属分类展示可选项
        assert tag_type_2.is_visible() and not tag_type_1.is_visible()

@pytest.mark.parametrize("unit",[tag_application_add_12,tag_application_edit_01])
@pytest.mark.run(order=706)
def test_tag_application_add_04(tag_application_module,unit):
    """ 新增编辑标签应用 """
    global tag_content
    session_page, handle = tag_application_module
    result = handle.add_handle(unit)
    tag_content = result['标签字段']
    handle.button_click("application-10008")  # 点击查询
    tag_type = handle.get_list_data("application-30001-t1")[0]
    tag_name = handle.get_list_data("application-30001-t2")[0]
    what_class = handle.get_list_data("application-30001-t3")[0].split('#')
    energy_type = handle.get_list_data("application-30001-t5")[0].split('、')
    assert tag_type == tag_content['标签类型'] and tag_name == tag_content['标签名称'] \
            and what_class == tag_content['所属分类'] and energy_type == tag_content['关联能源分类'] \
           and not session_page.get_by_role("alert").is_visible()

@pytest.mark.parametrize("unit",[tag_application_add_13,tag_application_add_14])
@pytest.mark.run(order=707)
def test_tag_application_add_05(tag_application_function,unit):
    """ 标签名称不可重复 """
    session_page, handle = tag_application_function
    handle.add_handle(unit)
    alert = session_page.get_by_role("alert")
    assert alert.is_visible() and alert.inner_text() == "标签已存在"

@pytest.mark.parametrize("unit",[tag_application_add_15,tag_application_add_16])
@pytest.mark.run(order=708)
def test_tag_application_add_06(tag_application_module,unit):
    """ 新增标签分类后验证标签分类是否可以被调用 """
    session_page, handle = tag_application_module
    result = handle.add_handle(unit)
    tag_name = session_page.get_by_role("menuitem", name=result['标签字段']['分类名称']).is_visible()
    assert  tag_name
    session_page.mouse.move(0, 0)
    session_page.mouse.click(x=0, y=0)

@pytest.mark.parametrize("unit",[tag_application_add_15["field_processing.标签字段"]["field_obj"]["分类名称"][-1],
                                 tag_application_add_16["field_processing.标签字段"]["field_obj"]["分类名称"][-1]])
@pytest.mark.run(order=709)
def test_tag_application_add_07(tag_application_module,unit):
    """ 新增标签分类后验证标签分类是否可以被查询和调用 """
    session_page, handle = tag_application_module
    handle.pull_checked("application-10002", "application-10002-0")  # 选择标签类型
    handle.element_click("application-10003")  # 点击所属分类
    handle.input("application-10003", unit)
    session_page.locator(f'[role="menuitem"][title="{unit}"]').locator('span').first.click()
    tag_name = session_page.get_by_role("menuitem", name=unit).is_visible()
    assert  tag_name
    handle.element_click("application-10007")  # 重置查询

@pytest.mark.parametrize("unit",["禁用","启用"])
@pytest.mark.run(order=710)
def test_tag_state_01(tag_application_module,unit):
    """ 验证编辑标签状态 """
    session_page, handle = tag_application_module
    text = tag_application_edit_01["field_processing.标签字段"]["field_obj"]["标签名称"][-1]
    edit_message = handle.action_bar(text,"application-30001-b1")
    handle.radio_select("application-form-10007", unit)  # 点击启用/禁用
    handle.button_click(text='确定')  # 点击确定按钮
    tag_state = handle.action_bar(text,"application-30001-t6")[-3] #再拿一次当前数据，只取标签状态
    print(text,"text")
    print(tag_state,"tag_state")
    assert tag_state == f"已{unit}" and f"已{unit}" not in edit_message

@pytest.mark.run(order=711)
def test_system_call_01(tag_application_module):
    """ 验证被调用标签的是否还可以被调用 """
    session_page, handle = tag_application_module
    handle.element_click("application-20001")  # 点击被调用标签
    call_tag = handle.get_values({"调用标签":"application-sys-call-10003"})["调用标签"].split('+')[0].split('#')[-1]
    handle.element_click("application-sys-call-10003")
    session_page.get_by_test_id("application-sys-call-10003").locator('input').fill(call_tag)
    assert session_page.locator(f'[role="menuitem"][title="{call_tag}"]').get_attribute("aria-disabled") == "true"
    handle.button_click(text='取消')

@pytest.mark.run(order=712)
def test_system_call_02(tag_application_module):
    """ 系统调用中禁用标签 """
    session_page, handle = tag_application_module
    handle.goto_local(url_path="/ins/#/tagManagement/library") # 跳转到标签库新增标签
    text = handle.add_handle(tag_system_call_01)["标签字段"]["标签名称"]
    handle.goto_local("application-20001","/ins/#/tagManagement/application")  # 回到标签应用页面
    handle.pull_checked("application-sys-call-10002", "application-sys-call-10002-1")  # 选择标签类型
    handle.element_click("application-sys-call-10003")
    handle.input("application-sys-call-10003", text)
    session_page.locator(f'[role="menuitem"][title="{text}"]').locator('span').first.click()
    handle.element_click("application-sys-call-10003",10)
    handle.radio_select("application-sys-call-10004", "禁用")
    handle.button_click(text='确定')
    tag_message = handle.action_bar(text,"application-30001-t6")
    print(tag_message,"tag_message")
    assert "已禁用" in tag_message and "已启用" not in tag_message

@pytest.mark.parametrize("sort",["升序","降序"])
@pytest.mark.run(order=713)
def test_tag_application_sort_01(tag_application_module,sort):
    """ 验证所属分类排序 """
    session_page, handle = tag_application_module
    sort_data,raw_data = handle.sort_handle(sort,('所属分类','application-30001-t3'))
    session_page.reload()
    if sort == "升序":
        assert "品牌" in sort_data[0]
    else:
        assert "智能" in sort_data[0]